﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
namespace Helper.Xml
{
    public static class XmlHelper
    {
        /// <summary>
        /// 键值对必须包含，"key" "value"
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="csspath"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetDic(string FilePath, string csspath)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(FilePath);
            XmlNodeList xn = doc.SelectNodes(csspath);
            Dictionary<string, string> dics = new Dictionary<string, string>();
            foreach (XmlNode n in xn)
            {
                string key = n.Attributes["key"].Value;
                string val = n.Attributes["value"].Value;
                dics[key] = val;
            }
            return dics;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="csspath"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetDic(XmlDocument doc, string csspath)
        {
            XmlNodeList xn = doc.SelectNodes(csspath);
            Dictionary<string, string> dics = new Dictionary<string, string>();
            foreach (XmlNode n in xn)
            {
                string key = n.Attributes["key"].Value;
                string val = n.Attributes["value"].Value;
                dics[key] = val;
            }
            return dics;
        }


        /// <summary>
        /// 单个属性
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="csspath"></param>
        /// <param name="attr"></param>
        /// <returns></returns>
        public static string GetAttr(string fileName, string csspath, string attr)
        {
            string rs = "";
            XmlDocument doc = new XmlDocument();
            doc.Load(fileName);
            XmlNode xn = doc.SelectSingleNode(csspath);
            if (xn != null)
            {
                rs = xn.Attributes[attr].Value;
            }
            return rs;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="csspath"></param>
        /// <param name="attr"></param>
        /// <returns></returns>
        public static string GetAttr(XmlDocument doc, string csspath, string attr)
        {
            string rs = "";
            XmlNode xn = doc.SelectSingleNode(csspath);
            if (xn != null)
            {
                rs = xn.Attributes[attr].Value;
            }
            return rs;
        }



    }
}
